package com.taoding.springbootbase.util;

import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil extends org.apache.commons.lang3.time.DateUtils{
	public static String getDatePoor(Date endDate, Date nowDate) {
		 
	    long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    long nm = 1000 * 60;
	    // long ns = 1000;
	    // 获得两个时间的毫秒时间差异
	    long diff = endDate.getTime() - nowDate.getTime();
	    // 计算差多少天
	    long day = diff / nd;
	    // 计算差多少小时
	    long hour = diff % nd / nh;
	    // 计算差多少分钟
	    long min = diff % nd % nh / nm;
	    // 计算差多少秒//输出结果
	    // long sec = diff % nd % nh % nm / ns;
	    return day+"天";
	}
		
	/**
	 * 当前年月日
	 * @param
	 * @param
	 * @return
	 */
	public static String currentDate(){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return  df.format(new Date());
	}
	/**
	 * 当前前一年 始终 年月日
	 * @param
	 * @param
	 * @return
	 */
	public static String[] lastDate(){
		DateFormat df = new SimpleDateFormat("yyyy");
		int lastYear = Integer.parseInt(df.format(new Date()));
		return  new String[]{lastYear-1+"-01-01",lastYear-1+"-12-31"};
	}
	/**
	 * 当前前一年 始终 年月
	 * @param
	 * @param
	 * @return
	 */
	public static String[] lastDateMonth(){
		DateFormat df = new SimpleDateFormat("yyyy");
		int lastYear = Integer.parseInt(df.format(new Date()));
		return  new String[]{lastYear-1+"-01",lastYear-1+"-12"};
	}
	//比较日期大小
		 public static int compare_date(String DATE1, String DATE2) {
		        
		        
		        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		        try {
		            Date dt1 = df.parse(DATE1);
		            Date dt2 = df.parse(DATE2);
		            if (dt1.getTime() > dt2.getTime()) {
		                return 1;
		            } else if (dt1.getTime() < dt2.getTime()) {
		                return -1;
		            } else {
		                return 0;
		            }
		        } catch (Exception exception) {
		            exception.printStackTrace();
		        }
		        return 0;
		    }
		 
		//比较日期大小
		 public static int compare_time(String DATE1, String DATE2) {
		        
		        
		        DateFormat df = new SimpleDateFormat("HH:mm");
		        try {
		            Date dt1 = df.parse(DATE1);
		            Date dt2 = df.parse(DATE2);
		            if (dt1.getTime() > dt2.getTime()) {
		                return 1;
		            } else if (dt1.getTime() < dt2.getTime()) {
		                return -1;
		            } else {
		                return 0;
		            }
		        } catch (Exception exception) {
		            exception.printStackTrace();
		        }
		        return 0;
		    }
		 
		 /**
		 	 * 取得当天日期是周几
		 	 * @param date
		 	 * @return
		 	 */
		 	public static String getWeekDay(Date date) {
		 		String week = null;
		 		Calendar c = Calendar.getInstance();
		 		c.setTime(date);
		 		int week_of_year = c.get(Calendar.DAY_OF_WEEK);
		 		switch (week_of_year - 1) {
		 		case 0:
					week = "周日";
					break;
				case 1:
					week = "周一";
					break;
				case 2:
					week = "周二";
					break;
				case 3:
					week = "周三";
					break;
				case 4:
					week = "周四";
					break;
				case 5:
					week = "周五";
					break;
				case 6:
					week = "周六";
					break;
				}
		 		return week;
		 	}

		 	/**
		 	 * 
		 	* @Title: getPassDayOfMonth 
		 	* @Description: TODO(取得当月已经过了多少天，也就是今天是几号) 
		 	* @param date
		 	*  
		 	* @return int    返回类型 
		 	* @throws
		 	 */
		 	public static int getPassDayOfMonth(Date date) {
		 		Calendar c = Calendar.getInstance();
		 				c.setTime(date);
		 				return c.get(Calendar.DAY_OF_MONTH);
		 	}
		 	
		 	
		 	public static String dayaddone(Date date){
		 		Format f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 		        Calendar c = Calendar.getInstance();
		 		        c.setTime(date);
		 		        c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天
		 		        Date tomorrow = c.getTime();
		 		        return f.format(tomorrow);


		 	}
		 	
		 	public static String dayreduceone(Date date){
		 		Format f = new SimpleDateFormat("yyyy-MM-dd");
		 		        Date today = new Date();
		 		        Calendar c = Calendar.getInstance();
		 		        c.setTime(today);
		 		        c.add(Calendar.DAY_OF_MONTH, -1);// 今天+1天
		 		        Date tomorrow = c.getTime();
		 		        return f.format(tomorrow);


		 	}
		 	
		 	/**
		 	 * 
		 	*  得到本月的第一天
		 	* @Title: getMonthFirstDay  
		 	*  
		 	* @return String    返回类型 
		 	* @throws
		 	 */
		 	public static String getMonthFirstDay() {
		 	Calendar calendar = Calendar.getInstance();
		 	calendar.set(Calendar.DAY_OF_MONTH,
		 	calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		 	SimpleDateFormat firstDay= new
		 	SimpleDateFormat("yyyy-MM-dd");
		 	return  firstDay.format(calendar.getTime());
		 	}

		 	/**
		 	 * 得到本月的最后一天
		 	*  
		 	* @Title: getMonthLastDay  
		 	*  
		 	* @return String    返回类型 
		 	* @throws
		 	 */
		 	public static String getMonthLastDay() {
		 	Calendar calendar = Calendar.getInstance();
		 	calendar.set(Calendar.DAY_OF_MONTH,
		 	calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		 	SimpleDateFormat lastDay= new
		 	SimpleDateFormat("yyyy-MM-dd");
		 	return  lastDay.format(calendar.getTime());
		 	}
		 	
		 	
		 	/**
		 	 * 
		 	*  得到上月的第一天
		 	* @Title: getMonthFirstDay  
		 	*  
		 	* @return String    返回类型 
		 	* @throws
		 	 */
		 	public static String getLastMonthFirstDay() {
		 		 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		 		         
		 		         Calendar   cal_1=Calendar.getInstance();//获取当前日期 
		 		         cal_1.add(Calendar.MONTH, -1);
		 		         cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
		 		         
		 	return  format.format(cal_1.getTime());
		 	}
		 	
		 	/**
		 	 * 
		 	*  得到上月的第一天
		 	* @Title: getMonthFirstDay  
		 	*  
		 	* @return String    返回类型 
		 	* @throws
		 	 */
		 	public static String getLastMonthFirstDay(String date) {
		 		try {
		 		
		 			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		 		         
		 			Calendar cal_1 = Calendar.getInstance();//获取当前日期 
					cal_1.setTime(format.parse(date));
					
					cal_1.add(Calendar.MONTH, -1);
	 		        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
			 		         
	 		        return  format.format(cal_1.getTime());
					
				} catch (ParseException e) {
					e.printStackTrace();
				}
 		         
 		        return null;
		 	}

		 	/**
		 	 * 得到上月的最后一天
		 	*  
		 	* @Title: getMonthLastDay  
		 	*  
		 	* @return String    返回类型 
		 	* @throws
		 	 */
		 	public static String getLastMonthLastDay() {
		 		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		 		Calendar cale = Calendar.getInstance();   
		 		cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天 
		 	return  format.format(cale.getTime());
		 	}
		 	
		 	
		 	
		 	/**
		 	 * 
		 	*  得到上季度的第一天
		 	* @Title: getMonthFirstDay  
		 	*  
		 	* @return String    返回类型 
		 	* @throws
		 	 */
		 	public static String getLastQuarterFirstDay() {
		 		 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		 		         
		 		         Calendar   cal_1=Calendar.getInstance();//获取当前日期 
		 		         cal_1.add(Calendar.MONTH, -3);
		 		         cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
		 		         
		 	return  format.format(cal_1.getTime());
		 	}
		 	/**
		 	 * 
		 	*  得到上季度的第一月
		 	* @Title: getMonthFirstDay  
		 	*  
		 	* @return String    返回类型 
		 	* @throws
		 	 */
		 	public static String getLastQuarterFirstMonth() {
		 		 SimpleDateFormat format = new SimpleDateFormat("MM"); 
		 		         
		 		         Calendar   cal_1=Calendar.getInstance();//获取当前日期 
		 		         cal_1.add(Calendar.MONTH, -3);
		 		         cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
		 		         
		 	return  format.format(cal_1.getTime());
		 	}

		 	/**
		 	 * 得到上季度的最后一天
		 	*  
		 	* @Title: getMonthLastDay  
		 	*  
		 	* @return String    返回类型 
		 	* @throws
		 	 */
		 	public static String getLastQuarterLastDay() {
		 		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		 		Calendar cale = Calendar.getInstance();   
		 		cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天 
		 	return  format.format(cale.getTime());
		 	}
		 	
		 	
		 	/**
		 	 * 得到上季度的最后一月
		 	*  
		 	* @Title: getMonthLastDay  
		 	*  
		 	* @return String    返回类型 
		 	* @throws
		 	 */
		 	public static String getLastQuarterLastMonth() {
		 		SimpleDateFormat format = new SimpleDateFormat("MM"); 
		 		Calendar cale = Calendar.getInstance();   
		 		cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天 
		 	return  format.format(cale.getTime());
		 	}
		 	/**
		 	 * 
		 	*  得到上年本月的第一天
		 	* @Title: getMonthFirstDay  
		 	*  
		 	* @return String    返回类型 
		 	* @throws
		 	 */
		 	public static String getLastYearMonthFirstDay() {
		 		 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		 		         
		 		         Calendar   cal_1=Calendar.getInstance();//获取当前日期 
		 		         cal_1.add(Calendar.YEAR, -1);
		 		         cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
		 		         
		 	return  format.format(cal_1.getTime());
		 	}

		 	/**
		 	 * 得到上年本月的最后一天
		 	*  
		 	* @Title: getMonthLastDay  
		 	*  
		 	* @return String    返回类型 
		 	* @throws
		 	 */
		 	public static String getLastYearMonthLastDay() {
		 		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		 		Calendar cale = Calendar.getInstance();   
		 		cale.add(Calendar.YEAR, -1);
		 		cale.set(Calendar.DAY_OF_MONTH,
		 				cale.getActualMaximum(Calendar.DAY_OF_MONTH));
		 	return  format.format(cale.getTime());
		 	}
		 	
		 	
		 	
		 	public static String format(Date date, String parsePattern) {
		 		
		 		SimpleDateFormat format = new SimpleDateFormat(parsePattern); 
		 		
		 		return  format.format(date);
		 	}	
		 	
		 	public static Date parse(String date, String parsePattern) {
		 		
		 		SimpleDateFormat format = new SimpleDateFormat(parsePattern); 
		 		
		 		try {
					return format.parse(date);
				} catch (ParseException e) {
					e.printStackTrace();
				}
		 		
		 		return null;
		 	}
		 	
		 	/**
			 * 
			 * 1 第一季度 2 第二季度 3 第三季度 4 第四季度
			 * 
			 * @param date
			 * @return
			 */
		 	public static int getSeason(String date) {
		 		return getSeason(parse(date, "yyyy-MM-dd"));
		 	}
		 	
		 	/**
			 * 
			 * 1 第一季度 2 第二季度 3 第三季度 4 第四季度
			 * 
			 * @param date
			 * @return
			 */
			public static int getSeason(Date date) {
		 
				int season = 0;
		 
				Calendar c = Calendar.getInstance();
				c.setTime(date);
				int month = c.get(Calendar.MONTH);
				switch (month) {
				case Calendar.JANUARY:
				case Calendar.FEBRUARY:
				case Calendar.MARCH:
					season = 1;
					break;
				case Calendar.APRIL:
				case Calendar.MAY:
				case Calendar.JUNE:
					season = 2;
					break;
				case Calendar.JULY:
				case Calendar.AUGUST:
				case Calendar.SEPTEMBER:
					season = 3;
					break;
				case Calendar.OCTOBER:
				case Calendar.NOVEMBER:
				case Calendar.DECEMBER:
					season = 4;
					break;
				default:
					break;
				}
				return season;
			}
			
			/**
			 * 获取两个日期之间的月份
			 * @param minDate
			 * @param maxDate
			 * @return
			 */
			public static List<String> getMonthBetween(String minDate, String maxDate) {
			    
			    try {
			    	
			    	ArrayList<String> result = new ArrayList<String>();
				    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

				    Calendar min = Calendar.getInstance();
				    Calendar max = Calendar.getInstance();
			    	
					min.setTime(sdf.parse(minDate));
					
					min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

				    max.setTime(sdf.parse(maxDate));
				    max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

				    Calendar curr = min;
				    
				    while (curr.before(max)) {
				    	
					     result.add(sdf.format(curr.getTime()));
					     curr.add(Calendar.MONTH, 1);
				    }

				    return result;
					
				} catch (ParseException e) {
					e.printStackTrace();
				}
			   
			    return Collections.EMPTY_LIST;
			}
			/**
			 * 判断季报
			 * @param targetValue
			 * @return
			 */
			public static boolean isQueterDeclare(Integer targetValue){
				Integer[] arr = new Integer[]{1,4,7,10};
			    return Arrays.asList(arr).contains(targetValue);
			}
			/**
			 * 判断是否是季报
			 * isQueterDeclare
			 * @param beginDateStr
			 * @param endDateStr
			 * @return
			 */
			public static boolean isQueterDeclare(String beginDateStr,String endDateStr){
				long day=0;
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				Date beginDate;
		        Date endDate;
		        try
		        {
		            beginDate = format.parse(beginDateStr);
		            endDate= format.parse(endDateStr);    
		            day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);    
		            
		        } catch (ParseException e)
		        {
		            // TODO 自动生成 catch 块
		            e.printStackTrace();
		        }   
				return day>31 && day<124 ?true:false;
			}
			//判断当前月份
			public static Integer getCurrentMonth(){
				Date date = new Date();
				SimpleDateFormat s = new SimpleDateFormat("MM");
				String ss = s.format(date);
				//Calendar cal = Calendar.getInstance();
				//Integer month = cal.get(Calendar.MONTH) + 1;
				
				return Integer.parseInt(ss);
			}
			//判断当前月份
			public static Integer getCurrentYear(){

				Date date = new Date();
				SimpleDateFormat s = new SimpleDateFormat("yyyy");
				String ss = s.format(date);
				//Calendar cal = Calendar.getInstance();
				//Integer year = cal.get(Calendar.YEAR) ;
				
				return Integer.parseInt(ss);
			}
			
			public static String getCurrentYearMonth() {
				 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); 
 		         
 		         Calendar   cal_1=Calendar.getInstance();//获取当前日期 
 		         cal_1.add(Calendar.MONTH, -1);
 		         cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
 		         
             	return  format.format(cal_1.getTime());
			}

		 	public static  void main(String args[]) throws ParseException{
//		 		System.out.println("2019-12-31".substring(0, 8));
		 		/*System.out.println(lastDate()[1]);
		 		System.out.println(JSON.toJSONString(getMonthBetween("2018-10-11", "2018-10-11")));
		 		System.out.println(currentDate());*/
		 		String currentYearMonth = getCurrentYearMonth();
		 		System.out.println(currentYearMonth);
			}
		 
}
